﻿using System ;
using System.Windows ;
using System.Collections ;
using System.Windows.Data ;

namespace OpenLightGroup.Common.Converters
    {
        /// <summary>
        /// Gets the number of items in the bound list and returns a Visibility Enum value.
        /// </summary>
        public class ListCountVisibilityConverter : IValueConverter
            {

                #region IValueConverter Members

                /// <summary>
                /// Gets the number of items in the bound list and returns a Visibility Enum value.
                /// </summary>
                /// <param name="value">An object that implements IList that is used to get the count.</param>
                /// <param name="targetType">Not used</param>
                /// <param name="parameter">Minimum number of items the IList must have to return Visible. If this parameter is null, 0 will be used.</param>
                /// <param name="culture"></param>
                /// <returns></returns>
                public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
                    {
                        if ( value != null )
                        {
                            if ( parameter == null )
                                parameter = 0 ;
                
                            int minCount = int.Parse( parameter.ToString( ), culture ) ;
                
                            int count ;  

                            IList list = value as IList ;

                            if ( list != null )
                            {
                                count = list.Count ;

                                // return (list.Count > minCount) ? Visibility.Visible : Visibility.Collapsed;
                            }
                            else
                            {
                   
                                if ( !int.TryParse( value.ToString( ), out count ) )
                                {
                                    count = -1 ;
                                }

                            }

                            return ( count > minCount ) ? Visibility.Visible : Visibility.Collapsed ;

                        }
                        return Visibility.Collapsed ;
                    }

                /// <summary>
                /// Not implemented.
                /// </summary>
                /// <param name="value"></param>
                /// <param name="targetType"></param>
                /// <param name="parameter"></param>
                /// <param name="culture"></param>
                /// <returns></returns>
                public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
                    {
                        throw new NotImplementedException( ) ;
                    }

                #endregion
            }

    }